/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     varButton.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/
#include "varButton.h"
#include "levetop.h"
#include "data.h"
#include "uiIcon.h"

/*variable adjustment*/
uint8_t Adj_count = 0;
uint8_t Adj_flag = 0;
uint8_t Adj_num = 0;
uint8_t Adj_time = 0;
uint8_t Adj_loop = 0;
Adj_Info gAdj_Info[ADJUST_SIZE];

/***********************************************************************************
 * Function Name : Adj_touch
 * Description   : Touch detection of variable adjustment.
 * Input         : None
 * Output        : None
 * Return        : None
 ************************************************************************************/
void Adj_touch(void)
{
	uint8_t i = 0;
	uint8_t rebuf[20];
	int16_t sum1 = 0;
	int8_t sum0 = 0;
	int32_t sum = 0;
	uint8_t num = 0;

	// press down
	if (gTpInfo.sta)
	{
		for (i = 0; i < Adj_count; i++)
		{
			if (gTpInfo.x[0] >= gAdj_Info[i].Xs && gTpInfo.y[0] >= gAdj_Info[i].Ys &&
				gTpInfo.x[0] <= gAdj_Info[i].Xe && gTpInfo.y[0] <= gAdj_Info[i].Ye &&
				Adj_flag == 0 && button_Press_flag == 0)
			{
				if (gAdj_Info[i].press_id != 0xFFFF)
				{
					Display_Icon(gAdj_Info[i].press_id, gAdj_Info[i].Xs, gAdj_Info[i].Ys, 0);
				}
				Adj_num = i;
				Adj_flag = 1;
				button_Press_flag = 1;
				if (gAdj_Info[i].key_mode == 1)
					Adj_loop = 1;

				if (buzzer)
					touch_buzzer();
			}
		}
	}

	// keep pressing
	if (((Adj_loop == 1 && Adj_time > 50)||(Adj_loop == 2 && Adj_time > 10)) && gTpInfo.sta == 1 && Adj_flag == 1)
	{
		Adj_loop = 2;
		Adj_time = 0;
		if (gAdj_Info[Adj_num].p_tpye == 0x00)
			sum = var[gAdj_Info[Adj_num].p_add * 2 + 1];
		else if (gAdj_Info[Adj_num].p_tpye == 0x80)
		{
			sum0 = var[gAdj_Info[Adj_num].p_add * 2 + 1];
			sum = sum0;
		}
		else if (gAdj_Info[Adj_num].p_tpye == 0x01)
			sum = (var[gAdj_Info[Adj_num].p_add * 2] << 8) + var[gAdj_Info[Adj_num].p_add * 2 + 1];
		else if (gAdj_Info[Adj_num].p_tpye == 0x81)
		{
			sum1 = (var[gAdj_Info[Adj_num].p_add * 2] << 8) + var[gAdj_Info[Adj_num].p_add * 2 + 1];
			sum = sum1;
		}
		else if (gAdj_Info[Adj_num].p_tpye == 0x02)
		{
			if (var[gAdj_Info[Adj_num].p_add * 2] >= 0x80)
				var[gAdj_Info[Adj_num].p_add * 2] = 0x40;
			sum = (var[gAdj_Info[Adj_num].p_add * 2] << 24) +
				  (var[gAdj_Info[Adj_num].p_add * 2 + 1] << 16) +
				  (var[gAdj_Info[Adj_num].p_add * 2 + 2] << 8) +
				  var[gAdj_Info[Adj_num].p_add * 2 + 3];
		}
		else if (gAdj_Info[Adj_num].p_tpye == 0x82)
			sum = (var[gAdj_Info[Adj_num].p_add * 2] << 24) +
				  (var[gAdj_Info[Adj_num].p_add * 2 + 1] << 16) +
				  (var[gAdj_Info[Adj_num].p_add * 2 + 2] << 8) +
				  var[gAdj_Info[Adj_num].p_add * 2 + 3];

		else if (gAdj_Info[Adj_num].p_tpye == 0x83)
		{
			sum = (var[gAdj_Info[Adj_num].p_add * 2 + 6] << 8) + var[gAdj_Info[Adj_num].p_add * 2 + 7];
			if (var[gAdj_Info[Adj_num].p_add * 2] >= 0x80)
				sum = sum + (0x80 << 24);
		}
		else if (gAdj_Info[Adj_num].p_tpye >= 0x40 && gAdj_Info[Adj_num].p_tpye <= 0x4F)
			sum = (var[gAdj_Info[Adj_num].p_add * 2] << 8) + var[gAdj_Info[Adj_num].p_add * 2 + 1];

		if (gAdj_Info[Adj_num].Adj_mode == 1) //+
		{
			if (gAdj_Info[Adj_num].p_tpye & 0x40)
			{
				num = gAdj_Info[Adj_num].p_tpye& 0x0F;
				if (sum & (1 << num))
					sum &= ~(1 << num);
				else
					sum |= (1 << num);
			}
			else
			{
				sum += gAdj_Info[Adj_num].Adj_step;

				if ((gAdj_Info[Adj_num].p_tpye & 0x80) > 0)
				{
					if (sum > gAdj_Info[Adj_num].V_max)
					{
						if (gAdj_Info[Adj_num].Return_Mode == 0)
							sum = gAdj_Info[Adj_num].V_max;
						else
							sum = gAdj_Info[Adj_num].V_min;
					}
					else if (sum < gAdj_Info[Adj_num].V_min)
					{
						sum = gAdj_Info[Adj_num].V_min;
					}
				}
				else
				{
					if (sum > gAdj_Info[Adj_num].V_max1)
					{
						if (gAdj_Info[Adj_num].Return_Mode == 0)
							sum = gAdj_Info[Adj_num].V_max1;
						else
							sum = gAdj_Info[Adj_num].V_min1;
					}
					else if (sum < gAdj_Info[Adj_num].V_min1)
					{
						sum = gAdj_Info[Adj_num].V_min1;
					}
				}
			}
		}
		else // -
		{
			if (gAdj_Info[Adj_num].p_tpye & 0x40)
			{
				num = gAdj_Info[Adj_num].p_tpye & 0x0F;
				if (sum & (1 << num))
					sum &= ~(1 << num);
				else
					sum |= (1 << num);
			}
			else
			{
				sum -= gAdj_Info[Adj_num].Adj_step;
				if ((gAdj_Info[Adj_num].p_tpye & 0x80) > 0)
				{
					if (sum < gAdj_Info[Adj_num].V_min)
					{
						if (gAdj_Info[Adj_num].Return_Mode == 0)
							sum = gAdj_Info[Adj_num].V_min;
						else
							sum = gAdj_Info[Adj_num].V_max;
					}
					else if (sum > gAdj_Info[Adj_num].V_max)
						sum = gAdj_Info[Adj_num].V_max;
				}
				else
				{
					if (sum < gAdj_Info[Adj_num].V_min1)
					{
						if (gAdj_Info[Adj_num].Return_Mode == 0)
							sum = gAdj_Info[Adj_num].V_min1;
						else
							sum = gAdj_Info[Adj_num].V_max1;
					}
					else if (sum > gAdj_Info[Adj_num].V_max1)
						sum = gAdj_Info[Adj_num].V_max1;
				}
			}
		}

		if (gAdj_Info[Adj_num].p_tpye == 0x00 || gAdj_Info[Adj_num].p_tpye == 0x80)
		{
			var[gAdj_Info[Adj_num].p_add * 2 + 1] = sum;
		}
		else if (gAdj_Info[Adj_num].p_tpye == 0x01 || gAdj_Info[Adj_num].p_tpye == 0x81)
		{
			var[gAdj_Info[Adj_num].p_add * 2] = sum >> 8 & 0xFF;
			var[gAdj_Info[Adj_num].p_add * 2 + 1] = sum & 0xFF;
		}
		else if (gAdj_Info[Adj_num].p_tpye == 0x02 || gAdj_Info[Adj_num].p_tpye == 0x82)
		{
			if (sum < 0)
			{
				var[gAdj_Info[Adj_num].p_add * 2] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 1] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 2] = sum >> 8 & 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 3] = sum & 0xFF;
			}
			else
			{
				var[gAdj_Info[Adj_num].p_add * 2] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 1] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 2] = sum >> 8 & 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 3] = sum & 0xFF;
			}
		}
		else if (gAdj_Info[Adj_num].p_tpye == 0x83)
		{
			if (sum < 0)
			{
				var[gAdj_Info[Adj_num].p_add * 2] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 1] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 2] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 3] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 4] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 5] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 6] = sum >> 8 & 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 7] = sum & 0xFF;
			}
			else
			{
				var[gAdj_Info[Adj_num].p_add * 2] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 1] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 2] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 3] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 4] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 5] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 6] = sum >> 8 & 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 7] = sum & 0xFF;
			}
		}
		else if (gAdj_Info[Adj_num].p_tpye >= 0x40 && gAdj_Info[Adj_num].p_tpye <= 0x4F)
		{
			var[gAdj_Info[Adj_num].p_add * 2] = sum >> 8 & 0xFF;
			var[gAdj_Info[Adj_num].p_add * 2 + 1] = sum & 0xFF;
		}

		if (gAdj_Info[Adj_num].p_add >= VAR_ADDR_S)
		{
			reg_operation(gAdj_Info[Adj_num].p_add);
		}
		page_date_update(pic_id);
	}

	if (Button_Key_Code_Flag == 1)
	{
		for (i = 0; i < Adj_count; i++)
		{
			if (gAdj_Info[i].Xs == 0xFFFF && gAdj_Info[i].Ys ==  Button_Key_Code)
			{
				Button_Key_Code_Flag = 0;
				Button_Key_Code = 0;
				if (gAdj_Info[i].p_tpye == 0x00)
					sum = var[gAdj_Info[i].p_add * 2 + 1];
				else if (gAdj_Info[i].p_tpye == 0x80)
				{
					sum0 = var[gAdj_Info[i].p_add * 2 + 1];
					sum = sum0;
				}
				else if (gAdj_Info[i].p_tpye == 0x01)
					sum = (var[gAdj_Info[i].p_add * 2] << 8) + var[gAdj_Info[i].p_add * 2 + 1];
				else if (gAdj_Info[i].p_tpye == 0x81)
				{
					sum1 = (var[gAdj_Info[i].p_add * 2] << 8) + var[gAdj_Info[i].p_add * 2 + 1];
					sum = sum1;
				}
				else if (gAdj_Info[i].p_tpye == 0x02)
				{
					if (var[gAdj_Info[i].p_add * 2] >= 0x80)
						var[gAdj_Info[i].p_add * 2] = 0x40;
					sum = (var[gAdj_Info[i].p_add * 2] << 24) +
						  (var[gAdj_Info[i].p_add * 2 + 1] << 16) +
						  (var[gAdj_Info[i].p_add * 2 + 2] << 8) +
						  var[gAdj_Info[i].p_add * 2 + 3];
				}
				else if (gAdj_Info[i].p_tpye == 0x82)
					sum = (var[gAdj_Info[i].p_add * 2] << 24) +
						  (var[gAdj_Info[i].p_add * 2 + 1] << 16) +
						  (var[gAdj_Info[i].p_add * 2 + 2] << 8) +
						  var[gAdj_Info[i].p_add * 2 + 3];
				else if (gAdj_Info[i].p_tpye == 0x83)
				{
					sum = (var[gAdj_Info[i].p_add * 2 + 6] << 8) + var[gAdj_Info[i].p_add * 2 + 7];
					if (var[gAdj_Info[i].p_add * 2] >= 0x80)
						sum = sum + (0x80 << 24);
				}
				else if (gAdj_Info[i].p_tpye >= 0x40 && gAdj_Info[i].p_tpye <= 0x4F)
					sum = (var[gAdj_Info[i].p_add * 2] << 8) + var[gAdj_Info[i].p_add * 2 + 1];

				if (gAdj_Info[i].Adj_mode == 1) //+
				{
					if (gAdj_Info[i].p_tpye & 0x40)
					{
						num = gAdj_Info[i].p_tpye & 0x0F;
						if (sum & (1 << num))
							sum &= ~(1 << num);
						else
							sum |= (1 << num);
					}
					else
					{
						sum += gAdj_Info[i].Adj_step;

						if ((gAdj_Info[i].p_tpye & 0x80) > 0)
						{
							if (sum > gAdj_Info[i].V_max)
							{
								if (gAdj_Info[i].Return_Mode == 0)
									sum = gAdj_Info[i].V_max;
								else
									sum = gAdj_Info[i].V_min;
							}
							else if (sum < gAdj_Info[i].V_min)
							{
								sum = gAdj_Info[i].V_min;
							}
						}
						else
						{
							if (sum > gAdj_Info[i].V_max1)
							{
								if (gAdj_Info[i].Return_Mode == 0)
									sum = gAdj_Info[i].V_max1;
								else
									sum = gAdj_Info[i].V_min1;
							}
							else if (sum < gAdj_Info[i].V_min1)
							{
								sum = gAdj_Info[i].V_min1;
							}
						}
					}
				}
				else // -
				{
					if (gAdj_Info[i].p_tpye & 0x40)
					{
						num = gAdj_Info[i].p_tpye & 0x0F;
						if (sum & (1 << num))
							sum &= ~(1 << num);
						else
							sum |= (1 << num);
					}
					else
					{
						sum -= gAdj_Info[i].Adj_step;
						if ((gAdj_Info[i].p_tpye & 0x80) > 0)
						{
							if (sum < gAdj_Info[i].V_min)
							{
								if (gAdj_Info[i].Return_Mode == 0)
									sum = gAdj_Info[i].V_min;
								else
									sum = gAdj_Info[i].V_max;
							}
							else if (sum > gAdj_Info[i].V_max)
								sum = gAdj_Info[i].V_max;
						}
						else
						{
							if (sum < gAdj_Info[i].V_min1)
							{
								if (gAdj_Info[i].Return_Mode == 0)
									sum = gAdj_Info[i].V_min1;
								else
									sum = gAdj_Info[i].V_max1;
							}
							else if (sum > gAdj_Info[i].V_max1)
								sum = gAdj_Info[i].V_max1;
						}
					}
				}

				if (gAdj_Info[i].p_tpye == 0x00 || gAdj_Info[i].p_tpye == 0x80)
				{

					var[gAdj_Info[i].p_add * 2 + 1] = sum & 0xFF;
				}
				else if (gAdj_Info[i].p_tpye == 0x01 || gAdj_Info[i].p_tpye == 0x81)
				{
					var[gAdj_Info[i].p_add * 2] = sum >> 8 & 0xFF;
					var[gAdj_Info[i].p_add * 2 + 1] = sum & 0xFF;
				}
				else if (gAdj_Info[i].p_tpye == 0x02 || gAdj_Info[i].p_tpye == 0x82)
				{

					if (sum < 0)
					{
						var[gAdj_Info[i].p_add * 2] = 0xFF;
						var[gAdj_Info[i].p_add * 2 + 1] = 0xFF;
						var[gAdj_Info[i].p_add * 2 + 2] = sum >> 8 & 0xFF;
						var[gAdj_Info[i].p_add * 2 + 3] = sum & 0xFF;
					}
					else
					{
						var[gAdj_Info[i].p_add * 2] = 0;
						var[gAdj_Info[i].p_add * 2 + 1] = 0;
						var[gAdj_Info[i].p_add * 2 + 2] = sum >> 8 & 0xFF;
						var[gAdj_Info[i].p_add * 2 + 3] = sum & 0xFF;
					}
				}
				else if (gAdj_Info[i].p_tpye == 0x83)
				{
					if (sum < 0)
					{
						var[gAdj_Info[i].p_add * 2] = 0xFF;
						var[gAdj_Info[i].p_add * 2 + 1] = 0xFF;
						var[gAdj_Info[i].p_add * 2 + 2] = 0xFF;
						var[gAdj_Info[i].p_add * 2 + 3] = 0xFF;
						var[gAdj_Info[i].p_add * 2 + 4] = 0xFF;
						var[gAdj_Info[i].p_add * 2 + 5] = 0xFF;
						var[gAdj_Info[i].p_add * 2 + 6] = sum >> 8 & 0xFF;
						var[gAdj_Info[i].p_add * 2 + 7] = sum & 0xFF;
					}
					else
					{
						var[gAdj_Info[i].p_add * 2] = 0;
						var[gAdj_Info[i].p_add * 2 + 1] = 0;
						var[gAdj_Info[i].p_add * 2 + 2] = 0;
						var[gAdj_Info[i].p_add * 2 + 3] = 0;
						var[gAdj_Info[i].p_add * 2 + 4] = 0;
						var[gAdj_Info[i].p_add * 2 + 5] = 0;
						var[gAdj_Info[i].p_add * 2 + 6] = sum >> 8 & 0xFF;
						var[gAdj_Info[i].p_add * 2 + 7] = sum & 0xFF;
					}
				}
				else if (gAdj_Info[i].p_tpye >= 0x40 && gAdj_Info[i].p_tpye <= 0x4F)
				{
					var[gAdj_Info[i].p_add * 2] = sum >> 8 & 0xFF;
					var[gAdj_Info[i].p_add * 2 + 1] = sum & 0xFF;
				}

				if (gAdj_Info[i].Code == 0xC003) // Enable serial port return
				{
					rebuf[3] = 0x41;
					if (gAdj_Info[i].p_tpye == 0x00 ||
						gAdj_Info[i].p_tpye == 0x80 ||
						gAdj_Info[i].p_tpye == 0x01 ||
						gAdj_Info[i].p_tpye == 0x81 ||
						(gAdj_Info[i].p_tpye >= 0x40 && gAdj_Info[i].p_tpye <= 0x4F))
					{
						rebuf[4] = gAdj_Info[i].p_add >> 8;
						rebuf[5] = gAdj_Info[i].p_add & 0xFF;
						rebuf[6] = var[gAdj_Info[i].p_add * 2];
						rebuf[7] = var[gAdj_Info[i].p_add * 2 + 1];
						Var_AddrToReg(&rebuf[4]);
						LT_InfoFeedback_with_addr(rebuf, 5);
					}
					else if (gAdj_Info[i].p_tpye == 0x02 || gAdj_Info[i].p_tpye == 0x82)
					{
						rebuf[4] = gAdj_Info[i].p_add >> 8;
						rebuf[5] = gAdj_Info[i].p_add & 0xFF;
						rebuf[6] = var[gAdj_Info[i].p_add * 2];
						rebuf[7] = var[gAdj_Info[i].p_add * 2 + 1];
						rebuf[8] = var[gAdj_Info[i].p_add * 2 + 2];
						rebuf[9] = var[gAdj_Info[i].p_add * 2 + 3];
						Var_AddrToReg(&rebuf[4]);
						LT_InfoFeedback_with_addr(rebuf, 7);
					}
					else if (gAdj_Info[i].p_tpye == 0x03 || gAdj_Info[i].p_tpye == 0x83)
					{
						rebuf[4] = gAdj_Info[i].p_add >> 8;
						rebuf[5] = gAdj_Info[i].p_add & 0xFF;
						rebuf[6] = var[gAdj_Info[i].p_add * 2];
						rebuf[7] = var[gAdj_Info[i].p_add * 2 + 1];
						rebuf[8] = var[gAdj_Info[i].p_add * 2 + 2];
						rebuf[9] = var[gAdj_Info[i].p_add * 2 + 3];
						rebuf[10] = var[gAdj_Info[i].p_add * 2 + 4];
						rebuf[11] = var[gAdj_Info[i].p_add * 2 + 5];
						rebuf[12] = var[gAdj_Info[i].p_add * 2 + 6];
						rebuf[13] = var[gAdj_Info[i].p_add * 2 + 7];
						Var_AddrToReg(&rebuf[4]);
						LT_InfoFeedback_with_addr(rebuf, 11);
					}
				}

				if (gAdj_Info[i].id != 0xFFFF)
				{
					Display_Icon(gAdj_Info[i].id, gAdj_Info[i].Xs, gAdj_Info[i].Ys, 0);
				}

				if (gAdj_Info[i].p_add >= VAR_ADDR_S)
					reg_operation(gAdj_Info[i].p_add);

				page_date_update(pic_id);
				break;
			}
		}
	}

	// raise
	if (gTpInfo.sta == 0 && Adj_flag == 1)
	{
		if (gAdj_Info[Adj_num].p_tpye == 0x00)
			sum = var[gAdj_Info[Adj_num].p_add * 2 + 1];
		else if (gAdj_Info[Adj_num].p_tpye == 0x80)
		{
			sum0 = var[gAdj_Info[Adj_num].p_add * 2 + 1];
			sum = sum0;
		}
		else if (gAdj_Info[Adj_num].p_tpye == 0x01)
			sum = (var[gAdj_Info[Adj_num].p_add * 2] << 8) + var[gAdj_Info[Adj_num].p_add * 2 + 1];
		else if (gAdj_Info[Adj_num].p_tpye == 0x81)
		{
			sum1 = (var[gAdj_Info[Adj_num].p_add * 2] << 8) + var[gAdj_Info[Adj_num].p_add * 2 + 1];
			sum = sum1;
		}
		else if (gAdj_Info[Adj_num].p_tpye == 0x02)
		{
			if (var[gAdj_Info[Adj_num].p_add * 2] >= 0x80)
				var[gAdj_Info[Adj_num].p_add * 2] = 0x40;
			sum = (var[gAdj_Info[Adj_num].p_add * 2] << 24) +
				  (var[gAdj_Info[Adj_num].p_add * 2 + 1] << 16) +
				  (var[gAdj_Info[Adj_num].p_add * 2 + 2] << 8) +
				  var[gAdj_Info[Adj_num].p_add * 2 + 3];
		}
		else if (gAdj_Info[Adj_num].p_tpye == 0x82)
			sum = (var[gAdj_Info[Adj_num].p_add * 2] << 24) +
				  (var[gAdj_Info[Adj_num].p_add * 2 + 1] << 16) +
				  (var[gAdj_Info[Adj_num].p_add * 2 + 2] << 8) +
				  var[gAdj_Info[Adj_num].p_add * 2 + 3];
		else if (gAdj_Info[Adj_num].p_tpye == 0x83)
		{
			sum = (var[gAdj_Info[Adj_num].p_add * 2 + 6] << 8) + var[gAdj_Info[Adj_num].p_add * 2 + 7];
			if (var[gAdj_Info[Adj_num].p_add * 2] >= 0x80)
				sum = sum + (0x80 << 24);
		}
		else if (gAdj_Info[Adj_num].p_tpye >= 0x40 && gAdj_Info[Adj_num].p_tpye <= 0x4F)
			sum = (var[gAdj_Info[Adj_num].p_add * 2] << 8) + var[gAdj_Info[Adj_num].p_add * 2 + 1];

		if (gAdj_Info[Adj_num].Adj_mode == 1) //+
		{
			if (gAdj_Info[Adj_num].p_tpye & 0x40)
			{
				num = gAdj_Info[Adj_num].p_tpye & 0x0F;
				if (sum & (1 << num))
					sum &= ~(1 << num);
				else
					sum |= (1 << num);
			}
			else
			{

				sum += gAdj_Info[Adj_num].Adj_step;
				if ((gAdj_Info[Adj_num].p_tpye & 0x80) > 0)
				{
					if (sum > gAdj_Info[Adj_num].V_max)
					{
						if (gAdj_Info[Adj_num].Return_Mode == 0)
							sum = gAdj_Info[Adj_num].V_max;
						else
							sum = gAdj_Info[Adj_num].V_min;
					}
					else if (sum < gAdj_Info[Adj_num].V_min)
					{
						sum = gAdj_Info[Adj_num].V_min;
					}
				}
				else
				{
					if (sum > gAdj_Info[Adj_num].V_max1)
					{
						if (gAdj_Info[Adj_num].Return_Mode == 0)
							sum = gAdj_Info[Adj_num].V_max1;
						else
							sum = gAdj_Info[Adj_num].V_min1;
					}
					else if (sum < gAdj_Info[Adj_num].V_min1)
					{
						sum = gAdj_Info[Adj_num].V_min1;
					}
				}
			}
		}
		else // -
		{
			if (gAdj_Info[Adj_num].p_tpye & 0x40)
			{
				num = gAdj_Info[Adj_num].p_tpye & 0x0F;
				if (sum & (1 << num))
					sum &= ~(1 << num);
				else
					sum |= (1 << num);
			}
			else
			{
				sum -= gAdj_Info[Adj_num].Adj_step;
				if ((gAdj_Info[Adj_num].p_tpye & 0x80) > 0)
				{
					if (sum < gAdj_Info[Adj_num].V_min)
					{
						if (gAdj_Info[Adj_num].Return_Mode == 0)
							sum = gAdj_Info[Adj_num].V_min;
						else
							sum = gAdj_Info[Adj_num].V_max;
					}
					else if (sum > gAdj_Info[Adj_num].V_max)
						sum = gAdj_Info[Adj_num].V_max;
				}
				else
				{
					if (sum < gAdj_Info[Adj_num].V_min1)
					{
						if (gAdj_Info[Adj_num].Return_Mode == 0)
							sum = gAdj_Info[Adj_num].V_min1;
						else
							sum = gAdj_Info[Adj_num].V_max1;
					}
					else if (sum > gAdj_Info[Adj_num].V_max1)
						sum = gAdj_Info[Adj_num].V_max1;
				}
			}
		}

		if (gAdj_Info[Adj_num].p_tpye == 0x00 || gAdj_Info[Adj_num].p_tpye == 0x80)
		{

			var[gAdj_Info[Adj_num].p_add * 2 + 1] = sum & 0xFF;
		}
		else if (gAdj_Info[Adj_num].p_tpye == 0x01 || gAdj_Info[Adj_num].p_tpye == 0x81)
		{
			var[gAdj_Info[Adj_num].p_add * 2] = sum >> 8 & 0xFF;
			var[gAdj_Info[Adj_num].p_add * 2 + 1] = sum & 0xFF;
		}
		else if (gAdj_Info[Adj_num].p_tpye == 0x02 || gAdj_Info[Adj_num].p_tpye == 0x82)
		{

			if (sum < 0)
			{
				var[gAdj_Info[Adj_num].p_add * 2] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 1] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 2] = sum >> 8 & 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 3] = sum & 0xFF;
			}
			else
			{
				var[gAdj_Info[Adj_num].p_add * 2] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 1] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 2] = sum >> 8 & 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 3] = sum & 0xFF;
			}
		}
		else if (gAdj_Info[Adj_num].p_tpye == 0x83)
		{
			if (sum < 0)
			{
				var[gAdj_Info[Adj_num].p_add * 2] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 1] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 2] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 3] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 4] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 5] = 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 6] = sum >> 8 & 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 7] = sum & 0xFF;
			}
			else
			{
				var[gAdj_Info[Adj_num].p_add * 2] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 1] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 2] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 3] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 4] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 5] = 0;
				var[gAdj_Info[Adj_num].p_add * 2 + 6] = sum >> 8 & 0xFF;
				var[gAdj_Info[Adj_num].p_add * 2 + 7] = sum & 0xFF;
			}
		}
		else if (gAdj_Info[Adj_num].p_tpye >= 0x40 && gAdj_Info[Adj_num].p_tpye <= 0x4F)
		{
			var[gAdj_Info[Adj_num].p_add * 2] = sum >> 8 & 0xFF;
			var[gAdj_Info[Adj_num].p_add * 2 + 1] = sum & 0xFF;
		}

		if (gAdj_Info[Adj_num].Code == 0xC003) // Enable serial port return
		{
			rebuf[3] = 0x41;
			if (gAdj_Info[Adj_num].p_tpye == 0x00 ||
				gAdj_Info[Adj_num].p_tpye == 0x80 ||
				gAdj_Info[Adj_num].p_tpye == 0x01 ||
				gAdj_Info[Adj_num].p_tpye == 0x81 ||
				(gAdj_Info[Adj_num].p_tpye >= 0x40 && gAdj_Info[Adj_num].p_tpye <= 0x4F))
			{
				rebuf[4] = gAdj_Info[Adj_num].p_add >> 8;
				rebuf[5] = gAdj_Info[Adj_num].p_add & 0xFF;
				rebuf[6] = var[gAdj_Info[Adj_num].p_add * 2];
				rebuf[7] = var[gAdj_Info[Adj_num].p_add * 2 + 1];
				Var_AddrToReg(&rebuf[4]);
				LT_InfoFeedback_with_addr(rebuf, 5);
			}
			else if (gAdj_Info[Adj_num].p_tpye == 0x02 || gAdj_Info[Adj_num].p_tpye == 0x82)
			{
				rebuf[4] = gAdj_Info[Adj_num].p_add >> 8;
				rebuf[5] = gAdj_Info[Adj_num].p_add & 0xFF;
				rebuf[6] = var[gAdj_Info[Adj_num].p_add * 2];
				rebuf[7] = var[gAdj_Info[Adj_num].p_add * 2 + 1];
				rebuf[8] = var[gAdj_Info[Adj_num].p_add * 2 + 2];
				rebuf[9] = var[gAdj_Info[Adj_num].p_add * 2 + 3];
				Var_AddrToReg(&rebuf[4]);
				LT_InfoFeedback_with_addr(rebuf, 7);
			}
			else if (gAdj_Info[Adj_num].p_tpye == 0x03 || gAdj_Info[Adj_num].p_tpye == 0x83)
			{
				rebuf[4] = gAdj_Info[Adj_num].p_add >> 8;
				rebuf[5] = gAdj_Info[Adj_num].p_add & 0xFF;
				rebuf[6] = var[gAdj_Info[Adj_num].p_add * 2];
				rebuf[7] = var[gAdj_Info[Adj_num].p_add * 2 + 1];
				rebuf[8] = var[gAdj_Info[Adj_num].p_add * 2 + 2];
				rebuf[9] = var[gAdj_Info[Adj_num].p_add * 2 + 3];
				rebuf[10] = var[gAdj_Info[Adj_num].p_add * 2 + 4];
				rebuf[11] = var[gAdj_Info[Adj_num].p_add * 2 + 5];
				rebuf[12] = var[gAdj_Info[Adj_num].p_add * 2 + 6];
				rebuf[13] = var[gAdj_Info[Adj_num].p_add * 2 + 7];
				Var_AddrToReg(&rebuf[4]);
				LT_InfoFeedback_with_addr(rebuf, 11);
			}
		}

		if (gAdj_Info[Adj_num].id != 0xFFFF)
		{
			Display_Icon(gAdj_Info[Adj_num].id, gAdj_Info[Adj_num].Xs, gAdj_Info[Adj_num].Ys, 0);
		}
		button_Press_flag = 0;
		Adj_flag = 0;
		Adj_loop = 0;
		Adj_time = 0;
		if (gAdj_Info[Adj_num].p_add >= VAR_ADDR_S)
		{
			reg_operation(gAdj_Info[Adj_num].p_add);
		}
		page_date_update(pic_id);
	}
}






